//
//  TPCityModel.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/10.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPCityModel.h"

@implementation TPCityModel

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    return @{@"uid":@"id"};
}
-(void)setCity:(NSString *)city{
    _city = city;
    [self phonetic:city];
}

- (void )phonetic:(NSString*)sourceString {
    
    NSMutableString *source = [sourceString mutableCopy];
    NSString *phonetic = @"#";
    if(source && sourceString.length>0)
        
    {
//        CFRange range = CFRangeMake(0, 1);
        CFStringTransform((__bridge CFMutableStringRef)source, 0, kCFStringTransformMandarinLatin, NO); //带音标
        
        CFStringTransform((__bridge CFMutableStringRef)source, 0, kCFStringTransformStripDiacritics, NO);
        
        phonetic = source;
        self.charCity = source; //全拼
        
        
        NSArray *array = [source componentsSeparatedByString:@" "];
        NSMutableString *att = [[NSMutableString alloc]initWithString:@""];
        for(NSString *string in array){
            [att appendString:[string substringToIndex:1]];
        }
        self.spell = att;
        
        phonetic = [phonetic substringToIndex:1];
        phonetic = [phonetic uppercaseString]; //capitalizedString
        
        int temp = [phonetic characterAtIndex:0];
        
        if (temp < 65 || temp > 122 || (temp > 90 && temp < 97)) {
            
            //不合法的title
            phonetic = @"#";
        }else{
            phonetic = phonetic;
        }
    }
    self.attic = phonetic;
    
}


@end


@implementation TPCitySouceModel
-(void)setSourceArray:(NSArray *)sourceArray{
    _sourceArray = sourceArray;
    [self getCityModelWithArray:sourceArray];
    
}

-(void)getCityModelWithArray:(NSArray *)array{
    
    NSArray *sourceArray = [NSArray yy_modelArrayWithClass:[TPCityModel class] json:array];
    self.allArray = [NSMutableArray arrayWithArray:sourceArray];
    
    self.sectionArray = [self getWithArray:sourceArray];
    
}

//把数组转成多数组并且排序
-(NSMutableArray *)getWithArray:(NSArray *)array{
    
    NSMutableArray *sectionArray = [NSMutableArray new];
    for(int ascii = 65; ascii<= 90; ascii++){
        
        NSMutableArray *ascArray = [[NSMutableArray alloc]init];
        for(TPCityModel *model in array){
             int temp = [model.attic characterAtIndex:0];
            
                if(temp == ascii){
                    
                    [ascArray addObject:model];
                    
                }
        }
        
        if(ascArray.count > 0)
            [sectionArray addObject:ascArray];

    }
    
     NSMutableArray *attcArray = [[NSMutableArray alloc]initWithObjects:@"#", nil];
    for(NSArray *array1 in sectionArray){
        TPCityModel *model = array1.firstObject;
        [attcArray addObject:model.attic];
    }
    _atticArray = attcArray;
    
    return sectionArray;
}

-(void)setSearchText:(NSString *)searchText{
    _searchText = searchText;
    [self getSearchText:searchText];
}


//搜索
-(void)getSearchText:(NSString *)searchText{
   

    [self.searchArray removeAllObjects];
    self.searchArray = [NSMutableArray array];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city CONTAINS[cd] %@", searchText];
    
    NSArray* friendArray = [_allArray filteredArrayUsingPredicate:predicate];
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"spell CONTAINS[cd] %@", searchText];
    NSArray* groupArray =  [_allArray filteredArrayUsingPredicate:predicate1];
    
    NSMutableArray *array = [NSMutableArray new];
    if(friendArray.count > 0){
        [array addObjectsFromArray:friendArray];
    }
    
    if(groupArray.count >0){
       [array addObjectsFromArray:groupArray];
    }
   
    if(array.count>0){
        self.searchArray = [self getWithArray:array];
        self.sectionArray = self.searchArray;
    }else{
        
        if(searchText.length == 0)
        self.sectionArray = [self getWithArray:self.allArray];
    }
    
}

@end
